﻿using SD.Common;
using SD.Infrastructure.EntityBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SlamDunk.MES.Domain.Entities.ResourceContext
{
    /// <summary>
    /// 班组
    /// </summary>
    public class Team : AggregateRootEntity
    {
        #region # 构造器

        #region 00.无参构造器
        /// <summary>
        /// 无参构造器
        /// </summary>
        protected Team()
        {
            //初始化导航属性
            this.Members = new HashSet<TeamMember>();
        }
        #endregion

        #region 01.创建班组构造器
        /// <summary>
        /// 创建班组构造器
        /// </summary>
        /// <param name="teamNo">班组编号</param>
        /// <param name="teamName">班组名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public Team(string teamNo, string teamName, Guid? facilityId, string description)
            : this()
        {
            base.Number = teamNo;
            base.Name = teamName;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #endregion

        #region # 属性

        #region 工厂模型Id —— Guid? FacilityId
        /// <summary>
        /// 工厂模型Id
        /// </summary>
        public Guid? FacilityId { get; private set; }
        #endregion

        #region 描述 —— string Description
        /// <summary>
        /// 描述
        /// </summary>
        public string Description { get; private set; }
        #endregion

        #region 导航属性 - 班组成员集 —— ICollection<TeamMember> Members
        /// <summary>
        /// 导航属性 - 班组成员集
        /// </summary>
        public virtual ICollection<TeamMember> Members { get; private set; }
        #endregion

        #endregion

        #region # 方法

        #region 修改班组 —— void UpdateInfo(string teamNo...
        /// <summary>
        /// 修改班组
        /// </summary>
        /// <param name="teamNo">班组编号</param>
        /// <param name="teamName">班组名称</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="description">描述</param>
        public void UpdateInfo(string teamNo, string teamName, Guid? facilityId, string description)
        {
            base.Number = teamNo;
            base.Name = teamName;
            this.FacilityId = facilityId;
            this.Description = description;

            //初始化关键字
            this.InitKeywords();
        }
        #endregion

        #region 设置班组成员 —— void SetMembers(IEnumerable<TeamMember> members)
        /// <summary>
        /// 设置班组成员
        /// </summary>
        /// <param name="members">班组成员集</param>
        public void SetMembers(IEnumerable<TeamMember> members)
        {
            members = members?.ToArray() ?? new TeamMember[0];

            this.ClearMembers();
            if (members.Any())
            {
                this.Members.AddRange(members);
                foreach (TeamMember teamMember in members)
                {
                    teamMember.Team = this;
                }
            }
        }
        #endregion

        #region 清空班组成员 —— void ClearMembers()
        /// <summary>
        /// 清空班组成员
        /// </summary>
        private void ClearMembers()
        {
            foreach (TeamMember teamMember in this.Members.ToArray())
            {
                this.Members.Remove(teamMember);
            }
        }
        #endregion

        #region 初始化关键字 —— void InitKeywords()
        /// <summary>
        /// 初始化关键字
        /// </summary>
        private void InitKeywords()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(base.Number);
            builder.Append(base.Name);
            builder.Append(this.Description);

            base.SetKeywords(builder.ToString());
        }
        #endregion

        #endregion
    }
}
